package io.renren.com.qywt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.com.qywt.dao.AftersaleProdDetailDao;
import io.renren.com.qywt.dto.AftersaleProdDetailDTO;
import io.renren.com.qywt.entity.AftersaleProdDetailEntity;
import io.renren.com.qywt.service.AftersaleProdDetailService;
import io.renren.commons.mybatis.service.impl.CrudServiceImpl;
import io.renren.commons.security.context.TenantContext;
import io.renren.commons.security.user.SecurityUser;
import io.renren.commons.tools.utils.ConvertUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 错发/少发明细表
 *
 * @author jiaohj 15110414840@139.com
 * @since 3.0 2022-08-25
 */
@Service
public class AftersaleProdDetailServiceImpl extends CrudServiceImpl<AftersaleProdDetailDao, AftersaleProdDetailEntity, AftersaleProdDetailDTO> implements AftersaleProdDetailService {

    @Override
    public QueryWrapper<AftersaleProdDetailEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<AftersaleProdDetailEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("tenant_code", TenantContext.getTenantCode(SecurityUser.getUser()));
        return wrapper;
    }


    @Override
    public List<AftersaleProdDetailDTO> getByWorkOrderId(Long workOrderId) {
        QueryWrapper<AftersaleProdDetailEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("work_order_id",workOrderId);
        wrapper.eq("tenant_code", TenantContext.getTenantCode(SecurityUser.getUser()));
        return ConvertUtils.sourceToTarget(baseDao.selectList(wrapper),AftersaleProdDetailDTO.class);
    }

    @Override
    public void deleteByWorkOrderId(Long workOrderId) {
        QueryWrapper<AftersaleProdDetailEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("work_order_id",workOrderId);
        wrapper.eq("tenant_code", TenantContext.getTenantCode(SecurityUser.getUser()));
        baseDao.delete(wrapper);
    }

    @Override
    public void addProdDetails(List<AftersaleProdDetailDTO> prodDetails) {
        if(!prodDetails.isEmpty()){
            List<AftersaleProdDetailEntity> list=ConvertUtils.sourceToTarget(prodDetails,AftersaleProdDetailEntity.class);
            super.insertBatch(list);
        }
    }
}